﻿Imports TNCDAL
Imports System.Xml

Public Class ucShipmentDetail
    Private m_ID_File As Integer
    Dim dc As New TanNamChinhDataContext

    Public Sub New(ByVal ID_File)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_ID_File = ID_File
    End Sub

    Private Sub ucShipmentDetail_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        KhoiTaoShipmentDetail(m_ID_File)
    End Sub

    Public Sub KhoiTaoShipmentDetail(ByVal ID_File As Integer)
        Dim q = (From p In dc.tblShipmentDetails
                Where p.ID_File = ID_File
                Select p).SingleOrDefault

        If q Is Nothing Then
            Clear()
        Else
            txtSoHopDong.Text = IIf(q.SoHopDong Is Nothing, "", q.SoHopDong)
            txtCoSoGiamDinh.Text = IIf(q.CoSoGiamDinh Is Nothing, "", q.CoSoGiamDinh)
            txtInvoice.Text = IIf(q.SoInvoice Is Nothing, "", q.SoInvoice)
            txtSoGiamDinh.Text = IIf(q.SoTiepNhan Is Nothing, "", q.SoTiepNhan)
            txtSoLuong.Text = CDbl(q.SoLuong).ToString("#,###")
            txtTriGia.Text = CDbl(q.TriGia).ToString("#,###")
            txtSoMauTiepNhan.Text = q.SoMauTiepNhan
            txtSoToKhai.Text = q.SoToKhai

            Try
                dtpNgayHopDong.Value = q.NgayHopDong
            Catch ex As Exception

            End Try

            Try
                dtpNgayGuiHoSo.Value = q.NgayGuiHoSo
            Catch ex As Exception

            End Try
            Try
                dtpNgayInvoice.Value = q.NgayInvoice
            Catch ex As Exception

            End Try
            Try
                dtpNgayToKhai.Value = q.NgayToKhai
            Catch ex As Exception

            End Try
            Try
                dtpNgayTraKetQua.Value = q.NgayTraKetQua
            Catch ex As Exception

            End Try

        End If

       
    End Sub

    Public Sub Clear()
        txtSoHopDong.Text = ""
        txtCoSoGiamDinh.Text = ""
        txtInvoice.Text = ""
        txtSoGiamDinh.Text = ""
        txtSoLuong.Text = "0"
        txtSoMauTiepNhan.Text = ""
        txtSoToKhai.Text = ""
        txtTriGia.Text = "0"

        dtpNgayHopDong.Value = Nothing
        dtpNgayGuiHoSo.Value = Nothing
        dtpNgayInvoice.Value = Nothing
        dtpNgayToKhai.Value = Nothing
        dtpNgayTraKetQua.Value = Nothing
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim q = (From p In dc.tblShipmentDetails
                Where p.ID_File = m_ID_File
                Select p).SingleOrDefault

        Dim SoLuong As Double = 0
        Dim TriGia As Double = 0
        Try
            SoLuong = txtSoLuong.Text
        Catch ex As Exception
            txtSoLuong.Text = ""
        End Try
        Try
            TriGia = txtTriGia.Text
        Catch ex As Exception
            txtTriGia.Text = ""
        End Try

        If q Is Nothing Then
            Dim objShip As New tblShipmentDetail
            objShip.ID_File = m_ID_File
            objShip.CoSoGiamDinh = txtCoSoGiamDinh.Text

            If dtpNgayGuiHoSo.Value = "#12:00:00 AM#" Then
                objShip.NgayGuiHoSo = Nothing
            Else
                objShip.NgayGuiHoSo = dtpNgayGuiHoSo.Value
            End If

            If dtpNgayHopDong.Value = "#12:00:00 AM#" Then
                objShip.NgayHopDong = Nothing
            Else
                objShip.NgayHopDong = dtpNgayHopDong.Value
            End If

            If dtpNgayInvoice.Value = "#12:00:00 AM#" Then
                objShip.NgayInvoice = Nothing
            Else
                objShip.NgayInvoice = dtpNgayInvoice.Value
            End If

            If dtpNgayToKhai.Value = "#12:00:00 AM#" Then
                objShip.NgayToKhai = Nothing
            Else
                objShip.NgayToKhai = dtpNgayToKhai.Value
            End If

            If dtpNgayTraKetQua.Value = "#12:00:00 AM#" Then
                objShip.NgayTraKetQua = Nothing
            Else
                objShip.NgayTraKetQua = dtpNgayTraKetQua.Value
            End If


            objShip.SoHopDong = txtSoHopDong.Text
            objShip.SoInvoice = txtInvoice.Text
            objShip.SoLuong = SoLuong
            objShip.SoMauTiepNhan = txtSoMauTiepNhan.Text
            objShip.SoTiepNhan = txtSoGiamDinh.Text
            objShip.SoToKhai = txtSoToKhai.Text
            objShip.TriGia = TriGia
            dc.tblShipmentDetails.InsertOnSubmit(objShip)
            dc.SubmitChanges()
        Else
            q.ID_File = m_ID_File
            q.CoSoGiamDinh = txtCoSoGiamDinh.Text
            'q.NgayGuiHoSo = dtpNgayGuiHoSo.Value
            'q.NgayHopDong = dtpNgayHopDong.Value
            'q.NgayInvoice = dtpNgayInvoice.Value
            'q.NgayToKhai = dtpNgayToKhai.Value
            'q.NgayTraKetQua = dtpNgayTraKetQua.Value
            q.SoHopDong = txtSoHopDong.Text
            q.SoInvoice = txtInvoice.Text
            q.SoLuong = SoLuong
            q.SoMauTiepNhan = txtSoMauTiepNhan.Text
            q.SoTiepNhan = txtSoGiamDinh.Text
            q.SoToKhai = txtSoToKhai.Text
            q.TriGia = TriGia

            If dtpNgayGuiHoSo.Value = "#12:00:00 AM#" Then
                q.NgayGuiHoSo = Nothing
            Else
                q.NgayGuiHoSo = dtpNgayGuiHoSo.Value
            End If

            If dtpNgayHopDong.Value = "#12:00:00 AM#" Then
                q.NgayHopDong = Nothing
            Else
                q.NgayHopDong = dtpNgayHopDong.Value
            End If

            If dtpNgayInvoice.Value = "#12:00:00 AM#" Then
                q.NgayInvoice = Nothing
            Else
                q.NgayInvoice = dtpNgayInvoice.Value
            End If

            If dtpNgayToKhai.Value = "#12:00:00 AM#" Then
                q.NgayToKhai = Nothing
            Else
                q.NgayToKhai = dtpNgayToKhai.Value
            End If

            If dtpNgayTraKetQua.Value = "#12:00:00 AM#" Then
                q.NgayTraKetQua = Nothing
            Else
                q.NgayTraKetQua = dtpNgayTraKetQua.Value
            End If

            dc.SubmitChanges()
        End If
    End Sub

    Private Sub txtSoLuong_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSoLuong.Leave
        Try
            txtSoLuong.Text = CDbl(txtSoLuong.Text).ToString("#,###")
        Catch ex As Exception
            txtSoLuong.Text = "0"
        End Try

    End Sub

    Private Sub txtTriGia_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTriGia.Leave
        Try
            txtTriGia.Text = CDbl(txtTriGia.Text).ToString("#,###")
        Catch ex As Exception
            txtTriGia.Text = "0"
        End Try
    End Sub
End Class
